/**
 * Copyright 2007 Andrea Agili <agea@users.sf.net>
 * 
 * This plugin is licensed under the Eclipse Public License:
 * http://www.eclipse.org/org/documents/epl-v10.php
 * 
 */

package org.dnd.rcp.securetexteditor.ui;

import java.io.File;

import org.dnd.rcp.securetexteditor.SecureEditor;
import org.dnd.rcp.securetexteditor.SecureEditorInput;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.ICoolBarManager;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.action.ToolBarContributionItem;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.ContributionItemFactory;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.plugin.AbstractUIPlugin;

public class ApplicationActionBarAdvisor extends ActionBarAdvisor {

	private Action newAction;

	private Action openAction;

	private Action showLogAction;

	private IWorkbenchAction saveAction;

	private IWorkbenchAction saveAllAction;

	private IWorkbenchAction saveAsAction;

	private IWorkbenchAction exitAction;

	private IWorkbenchAction aboutAction;

	private IContributionItem reopen;

	public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
		super(configurer);
	}

	protected void makeActions(final IWorkbenchWindow window) {
		saveAction = ActionFactory.SAVE.create(window);

		saveAllAction = ActionFactory.SAVE_ALL.create(window);

		saveAsAction = ActionFactory.SAVE_AS.create(window);

		exitAction = ActionFactory.QUIT.create(window);

		aboutAction = ActionFactory.ABOUT.create(window);

		reopen = ContributionItemFactory.REOPEN_EDITORS.create(window);

		openAction = new Action() {
			@Override
			public void run() {
				FileDialog dialog = new FileDialog(window.getShell(), SWT.OPEN);
				dialog.setFilterExtensions(new String[] { "*.uniatxt" });
				String filename = dialog.open();

				try {
					window.getWorkbench()
							.getActiveWorkbenchWindow()
							.getActivePage()
							.openEditor(
									new SecureEditorInput(new File(filename)),
									SecureEditor.ID);
				} catch (Exception e) {
					Activator
							.getDefault()
							.getLog()
							.log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
									IStatus.OK, "Error opening " + filename, e));
				}
			}
		};
		newAction = new Action() {
			@Override
			public void run() {
				try {
					window.getWorkbench()
							.getActiveWorkbenchWindow()
							.getActivePage()
							.openEditor(new SecureEditorInput(null),
									SecureEditor.ID);
				} catch (PartInitException e) {
					Activator
							.getDefault()
							.getLog()
							.log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
									IStatus.OK, "Error opening editor", e));
				}
			}
		};

		showLogAction = new Action() {
			@Override
			public void run() {
				try {
					window.getWorkbench().getActiveWorkbenchWindow()
							.getActivePage()
							.showView("org.eclipse.pde.runtime.LogView");
				} catch (PartInitException e) {
					Activator
							.getDefault()
							.getLog()
							.log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
									IStatus.OK, "Error opening log view", e));
				}
			}
		};

		openAction.setText("Open File");
		newAction.setText("New File");

		showLogAction.setText("Show error log");

		openAction.setImageDescriptor(AbstractUIPlugin
				.imageDescriptorFromPlugin(Activator.PLUGIN_ID,
						"icons/lock_open.png"));
		newAction.setImageDescriptor(AbstractUIPlugin
				.imageDescriptorFromPlugin(Activator.PLUGIN_ID,
						"icons/lock_add.png"));
		aboutAction.setImageDescriptor(AbstractUIPlugin
				.imageDescriptorFromPlugin(Activator.PLUGIN_ID,
						"icons/help.png"));
		showLogAction.setImageDescriptor(AbstractUIPlugin
				.imageDescriptorFromPlugin(Activator.PLUGIN_ID,
						"icons/page_white_error.png"));
		exitAction.setImageDescriptor(AbstractUIPlugin
				.imageDescriptorFromPlugin(Activator.PLUGIN_ID,
						"icons/door_out.png"));

		register(saveAction);
		register(saveAsAction);
		register(saveAllAction);
		register(exitAction);
		register(aboutAction);

	}

	protected void fillMenuBar(IMenuManager menuBar) {
		MenuManager fileMenu = new MenuManager("&File",
				IWorkbenchActionConstants.M_FILE);
		fileMenu.add(newAction);
		fileMenu.add(openAction);
		fileMenu.add(new Separator());
		fileMenu.add(saveAction);
		fileMenu.add(saveAsAction);
		fileMenu.add(saveAllAction);
		fileMenu.add(reopen);
		fileMenu.add(new Separator());
		fileMenu.add(showLogAction);
		fileMenu.add(new Separator());
		fileMenu.add(exitAction);
		menuBar.add(fileMenu);

		MenuManager aboutMenu = new MenuManager("&Help",
				IWorkbenchActionConstants.M_HELP);
		aboutMenu.add(aboutAction);
		menuBar.add(aboutMenu);
	}

	protected void fillCoolBar(ICoolBarManager coolBar) {
		IToolBarManager fileBar = new ToolBarManager(SWT.FLAT | SWT.LEFT);

		fileBar.add(newAction);
		fileBar.add(openAction);
		fileBar.add(saveAction);

		coolBar.add(new ToolBarContributionItem(fileBar, "file"));
	}

}
